//
//  StatusCellBottomView.swift
//  Weibo
//
//  Created by apple on 16/1/18.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit
import SnapKit

class StatusCellBottomView: UIView {
    
    @objc private func commentBtnDidClick() {
        let temp = TempViewController()
        //在页面跳转中 使用的频率非常高 可以代替绝大部分的页面跳转中使用协议代理 /  block
        //在push之前设置 隐藏底部 tabbar
        temp.hidesBottomBarWhenPushed = true
        navController()?.pushViewController(temp, animated: true)
    }
    
    //MARK: 定义外部变量
    
    //找到自定义的入口
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //MARK: 设置视图
    private func setupUI() {
        backgroundColor = UIColor.whiteColor()
        addSubview(retweetedBtn)
        addSubview(commentBtn)
        addSubview(likeBtn)
        
        
        //三等分一个视图
        //1.左右都应该设置约束条件
        //2.设置宽度相等
        //设置约束
        retweetedBtn.snp_makeConstraints { (make) -> Void in
            make.left.top.bottom.equalTo(self)
            
        }
        
        commentBtn.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(retweetedBtn.snp_right)
            make.bottom.top.equalTo(self)
            make.width.equalTo(retweetedBtn.snp_width)
        }
        likeBtn.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(commentBtn.snp_right)
            make.bottom.top.equalTo(self)
            make.right.equalTo(self.snp_right)
            make.width.equalTo(commentBtn.snp_width)
        }
        
        //Terminating app due to uncaught exception 'Cannot Install Constraint', reason: 'No common superview between views
        //原因: 没有添加到父视图上
        let sepView1 = lineView()
        
        let sepView2 = lineView()
        addSubview(sepView1)
        addSubview(sepView2)
        //设置约束
        
        let w: CGFloat = 0.5
        let scale: CGFloat = 0.4
        
        sepView1.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(retweetedBtn.snp_right)
            make.centerY.equalTo(self.snp_centerY)
            make.height.equalTo(self.snp_height).multipliedBy(scale)
            make.width.equalTo(w)    
        }
        
        sepView2.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(commentBtn.snp_right)
            make.centerY.equalTo(self.snp_centerY)
            make.height.equalTo(self.snp_height).multipliedBy(scale)
            make.width.equalTo(w)
        }
        
        //添加点击事件
        commentBtn.addTarget(self, action: "commentBtnDidClick", forControlEvents: .TouchUpInside)
        
        
    }
    
    private func lineView() -> UIView {
        let v = UIView()
        
        v.backgroundColor = UIColor.lightGrayColor()
        return v
    }
    
    //MARK: 懒加载子视图
    //转发微博的按钮
    private lazy var retweetedBtn: UIButton = UIButton(title: "转发", fontSize: 10, color: UIColor.darkGrayColor(), backImageName: nil, imageName: "timeline_icon_retweet")
    // 评论
    private lazy var commentBtn: UIButton = UIButton(title: "评论", fontSize: 10, color: UIColor.darkGrayColor(), backImageName: nil, imageName: "timeline_icon_comment")
    //点赞
    private lazy var likeBtn: UIButton = UIButton(title: "点赞", fontSize: 10, color: UIColor.darkGrayColor(), backImageName: nil, imageName: "timeline_icon_unlike")
}
